﻿Imports System.IO
Imports Microsoft.Win32

Public Class OptionForm
    Public Key As String
    Private ncode, kcode As Integer
    Private flag, kflag As Integer
    Private param, kparam As Integer

    Private Sub btnOK_Click(sender As Object, e As EventArgs) Handles btnOK.Click
        'Startup program file
        If StartCheck.CheckState = CheckState.Checked Then
            Startup.RunAtStartup("QuickLaucher", Application.StartupPath + "\" + AppDomain.CurrentDomain.FriendlyName)
        Else
            Startup.RemoveValue("QuickLaucher")
        End If
        'Process switch hotkey to ncode
        If ControlCombo.SelectedItem.ToString <> "" AndAlso KeyCombo.SelectedItem.ToString <> "" Then
            Select Case ControlCombo.SelectedItem.ToString
                Case "Alt"
                    param = 260
                    ncode = 164
                    flag = 32
                Case "Ctrl"
                    param = 256
                    ncode = 162
                    flag = 0
                Case "Shift"
                    param = 256
                    ncode = 160
                    flag = 0
            End Select

            Select Case KeyCombo.SelectedItem.ToString
                Case "Space"
                    kcode = 32
                Case "Tab"
                    kcode = 9
                Case "Esc"
                    kcode = 27
                Case Else
                    kcode = Asc(KeyCombo.SelectedItem.ToString)
            End Select
            kflag = 0
            kparam = 256
            Dim Hotkey As String = kcode.ToString + "-" + flag.ToString + "-" + param.ToString + "-" + ControlCombo.SelectedItem.ToString + "-" + KeyCombo.SelectedItem.ToString
            Startup.AddHotkey(Hotkey)
            MainForm.Prehotkey(Hotkey)
            Me.Close()
        Else
            MsgBox("Please, input valid hotkey!")
        End If
    End Sub

    Private Sub btnCancel_Click(sender As Object, e As EventArgs) Handles btnCancel.Click
        Me.Close()
        MainForm.Show()
    End Sub

    Private Sub OptionForm_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        'Initial hotkey
        Dim controlkeys As String() = {"Alt", "Ctrl", "Shift"}
        For Each controlkey As String In controlkeys
            ControlCombo.Items.Add(controlkey)
        Next

        Dim hotkeys As String() = {"Space", "Tab", "Esc"}
        For Each hotkey As String In hotkeys
            KeyCombo.Items.Add(hotkey)
        Next

        For i As Integer = 48 To 57
            KeyCombo.Items.Add(CChar(ChrW(i)))
        Next

        For i As Integer = 65 To 90
            KeyCombo.Items.Add(CChar(ChrW(i)))
        Next

        'Input select
        If My.Computer.Registry.GetValue("HKEY_CURRENT_USER\SOFTWARE\Microsoft\Windows\CurrentVersion\Run", "QuickLaucher", Nothing) Is Nothing Then
            StartCheck.CheckState = CheckState.Unchecked
        Else
            StartCheck.CheckState = CheckState.Checked
        End If

        ControlCombo.Text = MainForm.control
        KeyCombo.Text = MainForm.key
    End Sub
End Class